package com.jy.modules.cims.common.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.jy.modules.cims.common.sysUtils.ExceptionUtils;
import com.jy.modules.cims.data.common.dto.TCMmCostAffiliationDTO;
import com.jy.modules.cims.data.dto.InteractClassInvokeDto;
import com.jy.modules.cims.interact.service.InteractService;
import com.jy.platform.core.common.BaseDTO;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.exception.AbaboonException;
import com.jy.platform.restservice.web.base.BaseController;

/**
 * @classname: TCMmInteractClassInvokeController
 * @description: 定义 T_C_MM_INTERACT_CLASS_INVOKE 控制层
 * @author: dell
 */
@Controller
@Scope("prototype")
@RequestMapping("/tCMmInteractClassInvoke")
public class TCMmInteractClassInvokeController extends BaseController {
	private final String JSP_URL = "cims/tcmminteractclassinvoke/";

	private static final Logger logger = LoggerFactory.getLogger(TCMmInteractClassInvokeController.class);

	@Autowired
	@Qualifier("com.jy.modules.cims.interact.service.InteractService")
	private InteractService service;

	/**
	 * 通过 Controller 请求 跳转 进入至相关 页面
	 */
	@RequestMapping(value = "/prepareExecute/{operate}")
	public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
		ModelAndView model = new ModelAndView();
		if ("toQueryPage".equals(operate)) {// 跳转至 查询页面
			model.setViewName(JSP_URL + "queryTCMmInteractClassInvoke");
		} else if ("toAdd".equals(operate)) { // 跳转至 新增页面
			model.setViewName(JSP_URL + "addTCMmInteractClassInvoke");
		} else if ("toUpdate".equals(operate)) {// 跳转至 修改页面
			String id = this.getParameterString("id");
			model = this.queryOneDTO(id);
			model.setViewName(JSP_URL + "updateTCMmInteractClassInvoke");
		} else if ("toView".equals(operate)) {// 跳转至 查看页面
			String id = this.getParameterString("id");
			model = this.queryOneDTO(id);
			model.setViewName(JSP_URL + "viewTCMmInteractClassInvoke");
		}

		return model;
	}

	/**
	 * @author dell
	 * @description:查询分页列表
	 * @date 2015-05-14 13:12:50
	 * @param request
	 * @param dto
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryListTCMmInteractClassInvoke")
	@ResponseBody
	public DataMsg queryListTCMmInteractClassInvoke(HttpServletRequest request, InteractClassInvokeDto dto, @ModelAttribute DataMsg dataMsg)
			throws Exception {

		Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("dto", dto);
		QueryReqBean params = new QueryReqBean();
		params.setSearchParams(searchParams);
		PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);

		List<InteractClassInvokeDto> list = service.searchInteractClassInvokeByPaging(params.getSearchParams());

		dataMsg.setData(list);
		dataMsg.setTotalRows(pageInfo.getTotalCount());
		return dataMsg;
	}

	/**
	 * @author dell
	 * @description:编辑
	 * @date 2015-05-14 13:12:50
	 * @param request
	 * @param dto
	 * @param pageData
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/exeTCMmInteractClassInvoke")
	@ResponseBody
	public DataMsg exeTCMmInteractClassInvoke(HttpServletRequest request, @ModelAttribute DataMsg dataMsg) {
		try {
			String id = (String) this.getParameter("ids");

			String result = "";
			Object obj = service.exeInteractClassInvokeById(id);
			
			dataMsg = super.initDataMsg(dataMsg);
			dataMsg.setMsg("根据业务流水号执行接口信息成功" );
			
			if(null != obj){
				result = JSON.toJSON(obj).toString();
				dataMsg.setMsg("根据业务流水号执行接口信息成功:" + result);
			}
		} catch (Exception e) {
			String errorInfo = ExceptionUtils.getStackTraceAsString(e);
			dataMsg.failed(errorInfo);
			logger.error("执行方法异常：",errorInfo);
		}
		return dataMsg;
	}

	/**
	 * @author dell
	 * @description:通过主键查询 其明细信息
	 * @date 2015-05-14 13:12:50
	 * @param id
	 * @return
	 */
	private ModelAndView queryOneDTO(String id) throws AbaboonException {
		ModelAndView model = new ModelAndView();
		try {
			
			InteractClassInvokeDto dto = service.queryInteractClassInvokeByPrimaryKey(id);
			// 将信息放入 model 以便于传至页面 使用
			model.addObject("dto", dto);

		} catch (Exception e) {
			throw new AbaboonException("执行queryOneDTO异常：", e);
		}
		return model;
	}
	
	  /**
     * @author JY-IT-D001
     * @description:新增
     * @date 2016-04-11 11:12:31
     * @param request
     * @param dto
     * @param dataMsg
     * @return
     * @throws
     */
    @RequestMapping(value = "/insertTCMmInteractClassInvoke")
    @ResponseBody
    public DataMsg insertTCMmInteractClassInvoke(HttpServletRequest request, InteractClassInvokeDto dto, @ModelAttribute DataMsg dataMsg){
        try {
    		  service.insertInteractClassInvoke(dto);
              dataMsg = super.initDataMsg(dataMsg);
                dataMsg.setMsg("新增成功");
                dataMsg.setStatus("ok");
                dataMsg.setData(this.makeJSONData(dto.getId()));
        	
        }catch (Exception e) {
        	String errorInfo = ExceptionUtils.getStackTraceAsString(e);
        	dataMsg.failed(e.getMessage());
        	logger.error("执行方法insertSysResource异常：", errorInfo);
        }
        return dataMsg;
    }

}
